package ru.poomsae.isap.Online.FreestyleHosinsul

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.androidnetworking.AndroidNetworking
import com.androidnetworking.common.Priority
import com.androidnetworking.error.ANError
import com.androidnetworking.interfaces.JSONObjectRequestListener
import com.example.isap_wtf.Model.ApiEndPoint
import com.example.isap_wtf.Model.ParametrsISAP
import org.json.JSONObject
import ru.poomsae.isap.Online.Freestyle.Freestyle
import ru.poomsae.isap.R
import ru.poomsae.isap.VivOcenActivity

class HapkiPresentation : AppCompatActivity() {

    var cre = 0
    var har = 0
    var ene = 0
    var mus = 0

    var accy = 0.0
    var pres = 0.0

    var accyInt = 0
    var prInt = 0

    var onlineConSys = ParametrsISAP.onlineConSys
    var mIDHost = ParametrsISAP.idHostServer
    var idvistuplenie = ParametrsISAP.idvistuplenie
    var numSud = ParametrsISAP.numSud
    var sudID = ParametrsISAP.sudID

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.presentation_freestyle_hapkido)

        accy = intent.getDoubleExtra("accuracyDoubleFree", 0.0)
        val accyrFree = findViewById<TextView>(R.id.accyrFree)
        accyrFree.setText(accy.toString())

        val decorView = window.decorView
        val uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN

        if(onlineConSys == 1) { decorView.systemUiVisibility = uiOptions; getSupportActionBar()?.hide() }

        val creat = findViewById<RadioGroup>(R.id.creat)
        val creatFree = findViewById<TextView>(R.id.creatFree)
        val presFree = findViewById<TextView>(R.id.presFree)
        val totalFree = findViewById<TextView>(R.id.totalFree)
        creat.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                cre = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                pres = (cre + har + ene + mus).toDouble()/(10).toDouble()
                creatFree.setText(radio.text as String)
                presFree.setText(String.format("%.1f",pres))
                totalFree.setText(String.format("%.1f",pres+accy))
            })

        val harmony = findViewById<RadioGroup>(R.id.harmony)
        val harmonFree = findViewById<TextView>(R.id.harmonFree)
        harmony.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                har = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                pres = (cre + har + ene + mus).toDouble()/(10).toDouble()
                harmonFree.setText(radio.text as String)
                presFree.setText(String.format("%.1f",pres))
                totalFree.setText(String.format("%.1f",pres+accy))
            })

        val ener = findViewById<RadioGroup>(R.id.harmony)
        val enerFree = findViewById<TextView>(R.id.harmonFree)
        ener.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                ene = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                pres = (cre + har + ene + mus).toDouble()/(10).toDouble()
                enerFree.setText(radio.text as String)
                presFree.setText(String.format("%.1f",pres))
                totalFree.setText(String.format("%.1f",pres+accy))
            })

        val music_cher = findViewById<RadioGroup>(R.id.music_cher)
        val mus_cherFree = findViewById<TextView>(R.id.mus_cherFree)
        music_cher.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                mus = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                pres = (cre + har + ene + mus).toDouble()/(10).toDouble()
                mus_cherFree.setText(radio.text as String)
                presFree.setText(String.format("%.1f",pres))
                totalFree.setText(String.format("%.1f",pres+accy))
            })

        val sendFree = findViewById<Button>(R.id.sendFree)
        sendFree.setOnClickListener {
            if (onlineConSys == 0) {
                val intent = Intent (this, Freestyle::class.java)
                startActivity(intent)
                finish()
            } else {
                accyInt = (accy * 10).toInt()
                prInt = (pres * 10).toInt()
                addOcen()
            }

        }

    }

    fun addOcen() {
        AndroidNetworking.post("http://" + mIDHost + ApiEndPoint.ADDOCENKIA)
            .addBodyParameter("vistuplenie_idvistuplenie", idvistuplenie)
            .addBodyParameter("ocenki_nomsud", numSud)
            .addBodyParameter("sudID", ParametrsISAP.sudID)
            .addBodyParameter("ocenki_t", accyInt.toString())
            .addBodyParameter("ocenki_p", prInt.toString())
            .setPriority(Priority.MEDIUM)
            .build()
            .getAsJSONObject(object : JSONObjectRequestListener {
                override fun onResponse(response: JSONObject?) {
                    val success = response?.getInt("success")

                    Toast.makeText(applicationContext,"Judge  № " + numSud + " scores sent successfully success = " + success,
                        Toast.LENGTH_SHORT).show()
                }

                override fun onError(anError: ANError?) {
                    Toast.makeText(applicationContext,"Error judge  № " + numSud + " scores sent successfully",
                        Toast.LENGTH_SHORT).show()
                }
            })
        val intent = Intent (this, VivOcenActivity::class.java)
        startActivity(intent)
        finish()
    }
}